package com.aat.dxfy.spring.oss.start;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.aat.dxfy.spring.oss.service.TestService;
import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@ComponentScan(basePackages = { "com.aat.dxfy.spring.oss" })
public class TestStart {

	@Bean(initMethod = "init")
	DruidDataSource getDruidDataSource() {
		DruidDataSource bean = new DruidDataSource();
		bean.setDriverClassName("com.mysql.jdbc.Driver");
		bean.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8");
		bean.setUsername("root");
		bean.setPassword("root");
		bean.setInitialSize(1);
		bean.setMinIdle(1);
		bean.setMaxActive(10);
		return bean;
	}
	
	@Bean
	SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
		SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setTypeAliasesPackage("com.aat.dxfy.spring.oss.bean");
		return bean;
	}
	
	@Bean
	MapperScannerConfigurer getMapperScannerConfigurer(){
		MapperScannerConfigurer bean=new MapperScannerConfigurer();
		bean.setBasePackage("com.aat.dxfy.spring.oss.dao");
		return bean;
	}
	
	@Bean
	DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
		return new DataSourceTransactionManager(dataSource);
	}

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestStart.class);
		TestService test = context.getBean(TestService.class);
		System.out.println(test.queryBean("1"));
		context.close();
	}

}
